#!/bin/sh

#DB_HOST=
#DB_USER=
#DB_PASSWD=
#DB_PORT=

DB_CMD="/opt/vertica/bin/vsql -h $DB_HOST -U $DB_USER -w $DB_PASSWD -p $DB_PORT"

LOG_FILE=/tmp/`date +"%Y%m%d_%H%M"`.status

PRINT_HEADER() {
	echo "" > $LOG_FILE
	echo "================================================================================" >> $LOG_FILE
	echo "DB_HOST   : $DB_HOST" >> $LOG_FILE
	echo "DB_USER   : $DB_USER" >> $LOG_FILE
	echo "DB_PASSWD : $DB_PASSWD" >> $LOG_FILE
	echo "DB_PORT   : $DB_PORT" >> $LOG_FILE
	echo "--------------------------------------------------------------------------------" >> $LOG_FILE
	echo "" >> $LOG_FILE
}

RUN() {
	MSG=$1
	QUERY=$2
	
	echo "Collect info for" $MSG
	echo "================================================================================" >> $LOG_FILE
	echo "$MSG" >> $LOG_FILE
	echo "--------------------------------------------------------------------------------" >> $LOG_FILE
	$DB_CMD -c "$QUERY" >> $LOG_FILE
	echo "" >> $LOG_FILE
}

PRINT_HEADER
RUN "ROS Number" "select count(*) from storage_containers sc where sc.storage_type = 'ROS';"
RUN "CPU USAGE" "select * from cpu_usage where end_time=(select max(end_time) from cpu_usage) order by end_time desc, node_name;"
RUN "IO USAGE" "select * from io_usage where end_time=(select max(end_time) from io_usage) order by end_time desc, node_name;"
RUN "MEMORY USAGE" "select * from memory_usage where end_time=(select max(end_time) from memory_usage) order by end_time desc, node_name;"
RUN "NETWORK USAGE" "select * from network_usage where end_time=(select max(end_time) from network_usage) order by end_time desc, node_name;"
RUN "HOST_RESOURCES" "SELECT * FROM HOST_RESOURCES;"
RUN "NODE_RESOURCES" "SELECT * FROM NODE_RESOURCES;"
RUN "RESOURCE_POOLS" "SELECT * FROM RESOURCE_POOLS;"
RUN "SESSIONS" "SELECT * FROM SESSIONS;"
RUN "QUERY_METRICS" "SELECT * FROM QUERY_METRICS;"
##RUN "TUPLE_MOVER_OPERATIONS" "SELECT * FROM TUPLE_MOVER_OPERATIONS;"
RUN "RESOURCE_POOL_STATUS" "SELECT * FROM RESOURCE_POOL_STATUS;"
RUN "RESOURCE_USAGE" "SELECT * FROM RESOURCE_USAGE;"
RUN "STORAGE_USAGE" "SELECT * FROM STORAGE_USAGE;"
RUN "CONFIGURATION_CHANGES" "select * from configuration_changes;"
RUN "CONFIGURATION_PARAMETERS" "select * from configuration_parameters;"

echo ""
echo "Please see $LOG_FILE file for details."
echo ""
